﻿Public Class frmMoniteursAgence

    Private szSQL As String
    Private rs As ADODB.Recordset

    Private Sub frmMoniteursAgence_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitComboAgences(CB_Agences)
        ' afficher les affectations du moniteur
        InitListeAffectations(0)
    End Sub

    Private Sub InitListeAffectations(ByVal ix As Integer)
        Dim nb As Integer = 0

        ' effacer la listview
        LV_AFFECTATIONS.Items.Clear()

        ' Lire la table AFFECTATIONS MONITEURS
        szSQL = "CALL spGet_MONITEURS_AFF_byAGENCE(" & (CB_Agences.SelectedIndex + 1) & ",'" & Format(TE_DATE.Value, "yyyy/MM/dd") & "','" & _
                 Format(TE_DATE.Value, "yyyy/MM/dd") & "','ALL')"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_AFFECTATIONS.Items.Add(rs.Fields("cd_mon").Value)
            LV_AFFECTATIONS.Items(nb).SubItems.Add(rs.Fields("intitule").Value)
            LV_AFFECTATIONS.Items(nb).SubItems.Add(rs.Fields("dated").Value)
            LV_AFFECTATIONS.Items(nb).SubItems.Add(rs.Fields("datef").Value)
            LV_AFFECTATIONS.Items(nb).SubItems.Add(rs.Fields("dmatin").Value & "h à " & _
                                                   rs.Fields("fmatin").Value & "h")
            LV_AFFECTATIONS.Items(nb).SubItems.Add(rs.Fields("dsoir").Value & "h à " & _
                                                   rs.Fields("fsoir").Value & "h")
            LV_AFFECTATIONS.Items(nb).SubItems.Add(rs.Fields("no_ligne").Value)

            Dim debut As Date = rs.Fields("dated").Value
            Dim fin As Date = rs.Fields("datef").Value
            ' affectation obsolète ?
            If debut > Now Or fin < Now Then
                LV_AFFECTATIONS.Items(nb).ForeColor = Color.Gray
            End If
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()
        LB_NbLignes.Text = nb & " lignes lues"
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_AFFECTATIONS)

    End Sub

    Private Sub CB_Agences_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_Agences.SelectedIndexChanged
        ' afficher les affectations du moniteur
        InitListeAffectations(0)

    End Sub

    Private Sub TE_DATE_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TE_DATE.ValueChanged
        ' afficher les affectations du moniteur
        InitListeAffectations(0)

    End Sub

    Private Sub LV_AFFECTATIONS_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_AFFECTATIONS.DoubleClick

        If LV_AFFECTATIONS.SelectedItems.Count > 0 Then
            frmMoniteursMAJ.pNo_ligne = LV_AFFECTATIONS.SelectedItems(0).SubItems(6).Text
            ' afficher les détails de l'affectation
            frmMoniteursMAJ.ShowDialog()

            ' afficher les affectations du moniteur
            InitListeAffectations(LV_AFFECTATIONS.SelectedItems(0).Index)
        End If


    End Sub

End Class